package com.ebelter.bodyfatscale.ui.pager.register;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;

import com.ebelter.bodyfatscale.R;
import com.ebelter.bodyfatscale.model.RegisterInfo;
import com.ebelter.bodyfatscale.moudules.https.NetUtils;
import com.ebelter.bodyfatscale.moudules.https.interfaces.HttpResponse;
import com.ebelter.bodyfatscale.moudules.https.response.getEmailValidateCodeResponse;
import com.ebelter.bodyfatscale.ui.activity.BaseActivity;
import com.ebelter.bodyfatscale.ui.view.CountDownTextView;
import com.ebelter.bodyfatscale.util.ToastUtil;
import com.ebelter.bodyfatscale.util.VerifyUtils;
import com.ebelter.bodyfatscale.util.ViewUtils;
import com.ebelter.btlibrary.util.ULog;

/**
 * 注册页
 */

public class RegisterPager extends BasePager implements RadioGroup.OnCheckedChangeListener {


    private RadioGroup rgRole;
    private EditText etName;
    private EditText etMail;
    private EditText etMailCode;
    private EditText etPwd;
    private EditText etRepwd;
    private CountDownTextView page_register_countDownTv;//send 倒计时按钮
    private int role;

    private NetUtils myNetUtils;



    public RegisterPager(Context context) {
        super(context);
        myNetUtils = NetUtils.getInstance();
    }

    @Override
    protected int loadLayoutById() {
        return R.layout.pager_register;
    }

    @Override
    public void initViews() {

        rgRole = mRootView.findViewById(R.id.rg_role_register_pager);
        etName = mRootView.findViewById(R.id.et_name_register_pager);
        etMail = mRootView.findViewById(R.id.et_email_register_pager);
        etMailCode = mRootView.findViewById(R.id.et_emailcode_register_pager);
        etPwd = mRootView.findViewById(R.id.et_pwd_register_pager);
        etRepwd = mRootView.findViewById(R.id.et_repwd_register_pager);
        page_register_countDownTv = mRootView.findViewById(R.id.page_register_countDownTv);


//        //-----------昵称
//        etName.addTextChangedListener(new MyTextWatcher() {
//            @Override
//            public void afterTextChanged(Editable s) {
//                if (TextUtils.isEmpty(s.toString())){
//                    ViewUtils.shakeView(etName);
//                    ToastUtil.show(R.string.Nikename_cannot_be_empty);
//                }else {
////                    myNetUtils.validateEmailorNicknameRequest(mContext, s.toString(), false, new HttpResponse<ValidateEmailorNicknameResponse>() {
////                        @Override
////                        public void result(boolean isRequestSuccess, String requestDes, ValidateEmailorNicknameResponse validateEmailorNicknameResponse) {
////                            if (isRequestSuccess){
////                                if (validateEmailorNicknameResponse.resultCode==0){
////                                    //ToastUtil.show(validateEmailorNicknameResponse.validateStr +" " +StringUtils.getResStr(R.string.can_be_used), Gravity.CENTER);
////                                }else {
////                                   // ToastUtil.show(validateEmailorNicknameResponse.validateStr +" " +StringUtils.getResStr(R.string.have_been_registered), Gravity.CENTER);
////                                    ToastUtil.show(StringUtils.getResStr(R.string.nickname_have_been_registered), Gravity.CENTER);
////                                    ViewUtils.shakeView(etName);
////                                }
////                            }
////                        }
////                    });
//                }
//            }
//        });

        //-----------------邮箱
//        etMail.addTextChangedListener(new MyTextWatcher() {
//            @Override
//            public void afterTextChanged(Editable s) {
//                if (!VerifyUtils.emailFormat(s.toString())){
//                    //ViewUtils.shakeView(etMail);
//                   // ToastUtil.show(R.string.Incorrect_email_format);
//                }else {
//                    myNetUtils.validateEmailorNicknameRequest(mContext, s.toString(), true, new HttpResponse<ValidateEmailorNicknameResponse>() {
//                        @Override
//                        public void result(boolean isRequestSuccess, String requestDes, ValidateEmailorNicknameResponse validateEmailorNicknameResponse) {
//                            if (isRequestSuccess){
//                                if (validateEmailorNicknameResponse.resultCode==0){
//                                   // ToastUtil.show(validateEmailorNicknameResponse.validateStr +" " +StringUtils.getResStr(R.string.can_be_used), Gravity.CENTER);
//                                }else {
//                                    ToastUtil.show(validateEmailorNicknameResponse.validateStr +" " +StringUtils.getResStr(R.string.have_been_registered), Gravity.CENTER);
//                                    ViewUtils.shakeView(etMail);
//                                }
//                            }
//                        }
//                    });
//                }
//            }
//        });




//        //--------------------邮箱验证码
//        etMailCode.addTextChangedListener();



        rgRole.setOnCheckedChangeListener(this);

        //----------------------------------请求邮箱验证码按钮
        page_register_countDownTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email=etMail.getText().toString().trim();
                if (TextUtils.isEmpty(email)){
                    ViewUtils.shakeView(etMail);
                    ToastUtil.show(R.string.Email_must_not_be_empty);
                    return;
                }
                if (!VerifyUtils.emailFormat(email)){
                    ViewUtils.shakeView(etMail);
                    ToastUtil.show(R.string.Incorrect_email_format);
                }else {

                    if (!NetUtils.available()){
                        ToastUtil.show(R.string.Network_error);
                        return;
                    }

                    //showLoadingDialog(StringUtils.getResStr(R.string.Request_email_verification_code));
                    myNetUtils.getEmailValidateCodeRequest(mContext, email, 1, new HttpResponse<getEmailValidateCodeResponse>() {
                        @Override
                        public void result(boolean isRequestSuccess, String requestDes, getEmailValidateCodeResponse getEmailValidateCodeResponse) {
                           // cancelLoadingDialog();
                            if (isRequestSuccess){

                                if (getEmailValidateCodeResponse.resultCode==0){
                                    page_register_countDownTv.click();//说明请求成功，按钮进入倒计时模式
                                    ToastUtil.show(R.string.The_email_has_been_sent);
                                }else if (getEmailValidateCodeResponse.resultCode==102){//邮箱已经注册
                                    ToastUtil.show(R.string.Email_has_been_registered);
                                }else if (getEmailValidateCodeResponse.resultCode==110){//服务器异常
                                    ToastUtil.show(R.string.An_exception_occurred_on_the_server);
                                }else {
                                    ToastUtil.show(R.string.Failed_to_send_mail);
                                }

                            }else {
                                ToastUtil.show(R.string.Network_connection_error);
                            }
                        }
                    });
                }

            }
        });


    }

    private void showLoadingDialog(String str) {
        if (getActivity()!=null){
            ((BaseActivity)getActivity()).showLoadingDialog(str);
        }
    }

    private void cancelLoadingDialog(){
        if (getActivity()!=null){
            ((BaseActivity)getActivity()).dissLoadingDialog();
        }
    }


    //使昵称框抖动
    public void shake_etName(){
        ViewUtils.shakeView(etName);
    }

    //使邮箱框抖动
    public void shake_etMail(){
        ViewUtils.shakeView(etMail);
    }
    //使邮箱验证码框抖动
    public void shake_etMailCode(){
        ViewUtils.shakeView(etMailCode);
    }

    //使密码框抖动
    public void shake_etPwd(){
        ViewUtils.shakeView(etPwd);
        ViewUtils.shakeView(etRepwd);
    }



    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

        //TODO 角色定义记不起来了，可能是对的，请核对
        switch (checkedId) {
            case R.id.rb_worker_register_pager://普通
                role = 0;
                rgRole.setBackgroundResource(R.drawable.bg_role_worker);
                break;
            case R.id.rb_athlete_register_pager://运动员
                role = 1;
                rgRole.setBackgroundResource(R.drawable.bg_role_athlete);
                break;
        }
    }

    public RegisterInfo getRegisterInfo() {
        RegisterInfo mRegisterInfo = new RegisterInfo();

        mRegisterInfo.setName(etName.getText().toString().trim());
        mRegisterInfo.seteMail(etMail.getText().toString().trim());
        mRegisterInfo.setRole(role);
        mRegisterInfo.seteMailCode(etMailCode.getText().toString().trim());
        mRegisterInfo.setPwd(etPwd.getText().toString().trim());
        mRegisterInfo.setRepwd(etRepwd.getText().toString().trim());

        ULog.i(TAG, "----getRegisterInfo-----mRegisterInfo = " + mRegisterInfo);

        return mRegisterInfo;
    }

}
